<?php
namespace app\index\controller;

use function PHPSTORM_META\type;
use think\Db;
use think\Controller;
use think\Request;

class Index extends Controller
{
    public function index()
    {
        $first_sort = Db::table('first_sort')
            ->field('id,firstname')
            ->select();
        $second_sort =Db::table('second_sort')
            ->field('pid,secondname')
            ->select();
        $this->assign('first_sort',$first_sort);
        $this->assign('second_sort',$second_sort);
        return view();
    }

    public function goodslist()
    {
        return view();
    }







    public function buy()
    {
        $first_sort = Db::table('first_sort')
            ->field('id,firstname')
            ->select();
//        print_r($first_sort[0]['firstname']);

        $second_sort =Db::table('second_sort')
            ->field('pid,secondname')
            ->select();
        $yifu = Db::table('yifu')
            ->where('id',1)
            ->find();
        $shirt = $yifu['name'];
//        print_r($shirt);



//        根据前端传过来的颜色ID号,查询该颜色拥有的尺码号
        $shirtid = 1;
        $name_price=[];
        $all_color=[];
        $all_colors=[];
//      查询衣服名称'name','price','color','size','库存量'
        $shirt_name = Db::table('yifu')
            ->where('id',$shirtid)
            ->select();
        array_push($name_price,$shirt_name[0]['name'],$shirt_name[0]['price']);
        $color = Db::table('yi_color_size_id')
            ->field('color_id,sku')
            ->where('yi_id', $shirt_name[0]['id'])
            ->group('color_id')
            ->select();
//        print_r($color);

        //~获取颜色和sku
        foreach ($color as $s){
              array_push($all_colors,[$this->seeall($s['color_id']),$s['sku']]);
        }
//        print_r($all_colors);

        $this->assign('first_sort',$first_sort);
        $this->assign('second_sort',$second_sort);
        $this->assign('np',$name_price);
        $this->assign('ac',$all_colors);
        return view();
    }

    //    查询颜色
    private function seeall($s){
        $color = Db::table('color')
            ->field('color')
            ->where('id',$s)
            ->find();
        return $color['color'];
    }


    public function chan()
    {
        $request = $this->request;
        if ($request->isPost()){
//            $nameid = $request->post('name');
            $na = Request::post('name');
            return ['code'=>1,'info'=>'获取成功'];
        }else{
            return ['code'=>0,'info'=>'获取失败'];

        }

    }

    public function aa()
    {
        return view();
    }










}
